package com.mrslu.signkk.exception.action;


import cn.hutool.core.util.RandomUtil;
import com.mrslu.signkk.exception.ServiceException;
import com.mrslu.signkk.result.Result;
import com.mrslu.signkk.util.StringConstValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.util.CollectionUtils;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 全局异常处理
 *
 * @author 卢斌
 */
@Slf4j
@CrossOrigin
@ResponseBody
@ControllerAdvice
public class ExceptionHandler {


    @org.springframework.web.bind.annotation.ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Result<?> requestHead(HttpMediaTypeNotSupportedException e) {
        log.info("不支持的请求头");
        return Result.fail("不支持该请求头");
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(ServiceException.class)
    public Result<?> serviceException(ServiceException serviceException) {
        log.info("流程中断：{}", serviceException.getMessage());
        return Result.fail(serviceException.getMessage());
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(ServletRequestBindingException.class)
    public Result<?> paramError(ServletRequestBindingException e) {
        log.info("请求异常：", e);
        return Result.fail("请求参数有误");
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(HttpMessageConversionException.class)
    public Result<?> requestBodyError(HttpMessageConversionException e) {
        log.info("请求异常：", e);
        return Result.fail("无效的请求体参数");
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(ServletException.class)
    public Result<?> requestTypeError(ServletException e) {
        log.info("请求异常：", e);
        return Result.fail("无效的请求方式");
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public Result<?> exception(Exception e, HttpServletRequest request) {
        log.error(buildRequestPathLogContent(request));
        int code = RandomUtil.randomInt(100000, 999999);
        log.error("发生未能处理错误:{}", code, e);
        return Result.fail("系统异常:" + code);
    }


    public static String buildRequestPathLogContent(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        Map<String, String[]> storage = request.getParameterMap();
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("请求路径：").append(url);
        if (! CollectionUtils.isEmpty(storage)) {
            stringBuffer.append(StringConstValue.PARAM);
        }
        for(String i: storage.keySet()){
            stringBuffer.append(i).append(StringConstValue.EQUALS);
            String[] para = storage.get(i);
            if(para == null){
                continue;
            }
            for(String j:para){
                stringBuffer.append(j).append(StringConstValue.AND);
            }
        }
        if (StringConstValue.AND == stringBuffer.charAt(stringBuffer.length() - 1)) {
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        }
        return stringBuffer.toString();
    }


}